package com.cloud.pole.shop.service.goods.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cloud.pole.api.param.PicklistApi;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.PinyinUtils;
import com.cloud.pole.common.util.TreeUtil;
import com.cloud.pole.feign.IParamPickListService;
import com.cloud.pole.shop.biz.goods.ShopGoodsCategoryBiz;
import com.cloud.pole.shop.biz.shops.ShopShopsBiz;
import com.cloud.pole.shop.model.entity.goods.ShopGoodsCategory;
import com.cloud.pole.shop.model.entity.shops.ShopShopsWithBLOBs;
import com.cloud.pole.shop.model.vo.goods.GoodsCategoryTree;
import com.cloud.pole.shop.service.goods.GoodsCategoryService;

import lombok.extern.slf4j.Slf4j;

/*
*2019年3月14日
*cloud-shop
*author:frank-chen
*/
@Slf4j
@Service
public class GoodsCategoryServiceImpl implements GoodsCategoryService {

	@Autowired 
	ShopGoodsCategoryBiz shopGoodsCategoryBiz;
	@Autowired 
	private ShopShopsBiz shopShopsBiz;
	@Autowired
	private IParamPickListService iParamPickListService;
	
	
	@Override
	public List<GoodsCategoryTree> getAllTreeList() throws Exception {
		// TODO Auto-generated method stub
		return getGoodsCategoryTree(shopGoodsCategoryBiz.selectAllList(), 0);
	}
	
	/**
	 * 转换为树结构
	 * @param categorys
	 * @param root
	 * @return
	 */
    private List<GoodsCategoryTree> getGoodsCategoryTree(List<ShopGoodsCategory> categorys,int root) {
        List<GoodsCategoryTree> trees = new ArrayList<GoodsCategoryTree>();
        GoodsCategoryTree node = null;
        for (ShopGoodsCategory category : categorys) {
            node = new GoodsCategoryTree();
            BeanUtils.copyProperties(category, node);
            node.setId(category.getCategoryId());
            node.setParentId(category.getSupId());
            trees.add(node);
        }
        return TreeUtil.bulid(trees,root) ;
    }

	@Override
	public List<GoodsCategoryTree> getTreeListByShopId(Integer shopId) throws Exception {
		return getGoodsCategoryTree(shopGoodsCategoryBiz.selectListByShopId(shopId), 0);
	}

	@Override
	public void addGoodsCategory(String token, ShopGoodsCategory category) throws Exception {
		// TODO Auto-generated method stub
		//查询shop信息
		ShopShopsWithBLOBs shopInfo = shopShopsBiz.selectById(category.getShopId());
		//查询上级Category
		ShopGoodsCategory superCategory = null;
		if(category.getSupId() != null && category.getSupId() > 0) {
			superCategory = shopGoodsCategoryBiz.selectById(category.getSupId());
		}
		if(StringUtils.isEmpty(category.getKeyword())) {
			category.setKeyword(category.getName());
		}
		category.setMerchantId(shopInfo.getMerchantId());
		if(superCategory != null) {
			category.setCateLevel(superCategory.getCateLevel() + 1);
		}
		else {
			category.setCateLevel(1);
		}
		category.setIcode(PinyinUtils.ToFirstChar(category.getName()));
		ApiRestResponse<PicklistApi> selectByCodeVal = iParamPickListService.selectByCodeVal("cateAdvertLinkObjType",
				"1");
		PicklistApi picklistApi = selectByCodeVal.getData();
		category.setAdvertLinkObjType(Integer.valueOf(picklistApi.getPicklistValue()));
		category.setAdvertLinkObjTypeName(picklistApi.getPicklistText());
		System.out.println("添加的对象:"+category);
		shopGoodsCategoryBiz.insertUseGeneratedKeys(category);
		
		//更新code
		if(superCategory != null) {
			category.setCode(superCategory.getCode() + "-" + Integer.toString(category.getCategoryId()));
		}
		else {
			category.setCode(Integer.toString(category.getCategoryId()));
		}
		shopGoodsCategoryBiz.updateSelectiveById(category);
	}

	@Override
	public List<ShopGoodsCategory> selectByTop() {
		return shopGoodsCategoryBiz.selectByOne();
	}

	@Override
	public List<ShopGoodsCategory> sleectByCode(String supId) {
		return shopGoodsCategoryBiz.selectByCodeGoods(supId);
	}
}
